﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LL.Business;
using NanoERP.Business;
using NanoERP.Model.Poco;
using Omu.ValueInjecter;
//using NanoERP.Data;
//using NanoERP.Data.Interface;
namespace NanoERP.Business.Permission
{

    public class ModuleRightService : ServiceBase
    {
        public IList<string> GetModuleRights(Guid userId, string moduleCode)
        {
            var str = "SELECT RightCode FROM  dbo.V_RightCode WHERE UserId=@0 AND ModuleCode=@1";
            return DbSession.SqlQuery<string>(str, userId, moduleCode);
        }

        public IList<string> GetModulesRights(Guid userId, string modulesCode)
        {
            var str = string.Format("SELECT RightCode FROM  dbo.V_RightCode WHERE UserId=@0 AND ModuleCode IN ({0})", modulesCode);
            return DbSession.SqlQuery<string>(str, userId);
        }

        #region MyRegion
        ///// <summary>
        ///// 获取用户的角色
        ///// </summary>
        ///// <param name="userId"></param>
        ///// <returns></returns>
        //public RightTreeModel GetRightsForUser(Guid userId)
        //{
        //    var str = new StringBuilder();
        //    str.AppendLine("SELECT  SYS_Right.* ,");
        //    str.AppendLine("CONVERT(BIT,CASE WHEN dbo.SYS_Rights_Users.RightId IS NULL ");
        //    str.AppendLine("THEN 0 ELSE 1 END) Checked");
        //    str.AppendLine("FROM dbo.SYS_User");
        //    str.AppendLine("CROSS JOIN dbo.SYS_Right");
        //    str.AppendLine("LEFT JOIN SYS_Rights_Users ON dbo.SYS_User.Id = dbo.SYS_Rights_Users.UserId");
        //    str.AppendLine("AND dbo.SYS_Right.Id = dbo.SYS_Rights_Users.RightId");
        //    str.AppendLine("WHERE dbo.SYS_User.Id=@0 ORDER BY Sort");
        //    var Rights = this.DbSession.SqlQuery<RightModel>(str.ToString(), userId);
        //    var tree = this.GetWholeRightTypeTree();

        //    foreach (var RightModel in
        //        (from d in Rights where d.TypeId == null select d))
        //    {
        //        tree.Rights.Add(RightModel);
        //    }
        //    Fill(tree.Children, Rights);
        //    return tree;
        //}

        //private void Fill(IEnumerable<RightTreeModel> iList, IList<RightModel> Rights)
        //{
        //    foreach (var model in iList)
        //    {
        //        RightTreeModel model1 = model;
        //        foreach (var Right in
        //            (from d in Rights where d.TypeId == model1.Id select d))
        //        {
        //            model.Rights.Add(Right);
        //        }
        //        Fill(model.Children, Rights);
        //    }
        //}

        //public void FillRightsForDepartmentUsers(IList<DepartmentTree> deps, bool recursion)
        //{
        //    foreach (var department in deps)
        //    {
        //        FillRightsForDepartmentUsers(department, recursion);
        //        FillRightsForDepartmentUsers(department.Children, recursion);
        //    }
        //}

        //public void FillRightsForDepartmentUsers(DepartmentTree department, bool recursion)
        //{
        //    //throw new NotImplementedException();
        //    //foreach (var user in department.Users)
        //    //{
        //    //    var userList = GetRightsForUser(user.Id);
        //    //    foreach (var RightModel in userList)
        //    //    {
        //    //        user.Rights.Add(RightModel);
        //    //    }
        //    //}
        //}

        //public void FillRightsForRightTree(IList<RightTreeModel> RightTree, bool recursion)
        //{
        //    foreach (var Right in RightTree)
        //    {
        //        FillRightsForRightTree(Right, recursion);
        //        FillRightsForRightTree(Right.Children, recursion);
        //    }
        //}

        //public void FillRightsForRightTree(RightTreeModel RightTree, bool recursion)
        //{

        //    ////var dao = this.DbSession;
        //    //var list = this
        //    //    .GetRightsByRightType(RightTree.Id);
        //    //foreach (var Right in list)
        //    //{
        //    //    RightTree.Rights.Add(Right);
        //    //}
        //}

        //public IList<SYS_Right> GetRights(Guid userId)
        //{
        //    var str = new StringBuilder();
        //    str.AppendFormat("select {0}.* from {0} inner join SYS_Rights_Users ", "SYS_Right");
        //    str.AppendFormat("on {0}.Id=SYS_Rights_Users.RightId ", "SYS_Right");
        //    str.AppendFormat("where SYS_Rights_Users.UserId=@0 ");
        //    return this.DbSession.SqlQuery<SYS_Right>(str.ToString(), userId);
        //}

        //public IList<SYS_Right> GetRightsByRightType(Guid typeId)
        //{
        //    return this.DbSession.Query<SYS_Right>("TypeId=@0", typeId);
        //}

        //public void AddRight(SYS_Right Right)
        //{
        //    this.DbSession.Insert(Right);
        //}

        //public void DeleteRight(Guid RightId)
        //{
        //    var dao = this.DbSession.Delete<SYS_Right>(RightId);
        //}

        //public void UpdateRight(SYS_Right Right)
        //{
        //    this.DbSession.Update(Right);
        //}

        //public void Move(Guid RightId, Guid? RightTypeId)
        //{
        //    var dao = this.DbSession;
        //    var Right = dao.Get<SYS_Right>(RightId);
        //    Right.TypeId = RightTypeId;
        //    dao.Update(Right);
        //}

        //public void SetRightRights(Guid RightId, IList<Guid> rights)
        //{
        //    var sqlHelper = this.DbSession;
        //    sqlHelper.ExcuteNonQuery("delete from rights_Right where RightId=@0", RightId);
        //    foreach (Guid right in rights)
        //    {
        //        sqlHelper.ExcuteNonQuery("insert into rights_Right(RightId,rightId) VALUES(@0,@1)", RightId, right);
        //    }
        //}

        //#region RightType

        //public RightTreeModel GetWholeRightTypeTree()
        //{
        //    var list = this.GetTopRightTypes();
        //    var root = new RightTreeModel() { Name = "角色" };
        //    foreach (var item in list)
        //    {
        //        var newItem = new RightTreeModel();
        //        newItem.InjectFrom(item);
        //        FillSubRightTree(newItem, true);
        //        root.Children.Add(newItem);
        //    }
        //    return root;
        //}

        //public IList<SYS_RightType> GetTopRightTypes()
        //{
        //    return this.DbSession.Query<SYS_RightType>("ParentId is null Order By Sort");
        //}

        //public void FillSubRightTree(RightTreeModel tree, bool recursion = false)
        //{
        //    var list = this.GetRightTypesByParentId(tree.Id);
        //    foreach (var item in list)
        //    {
        //        var newItem = new RightTreeModel();
        //        newItem.InjectFrom(item);
        //        if (recursion)
        //            FillSubRightTree(newItem, true);
        //        tree.Children.Add(newItem);
        //    }
        //}

        //public IList<SYS_RightType> GetRightTypesByParentId(Guid RightTypeId)
        //{
        //    return this.DbSession.Query<SYS_RightType>("ParentId=@0 Order By Sort", RightTypeId);
        //}

        //#endregion

        #endregion
    }
}
